<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Akad_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
    }

    function getMahasiswa($nim)
	{
	    $query = "SELECT `STATUS` FROM mahasiswa WHERE `NIM`='$nim' LIMIT 1";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }
        else $data = NULL;

        return $data;
	}

	function getDataMahasiswa($nim)
	{
	    $query = "SELECT `KODE_PRODI` FROM view_data_mahasiswa WHERE `NIM`='$nim' LIMIT 1";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }
        else $data = NULL;

        return $data;
	}

    function getJadwalKuliah($kode_prodi,$kode_semester,$kode_tahun,$id_hari)
	{

	    $query = "SELECT * FROM view_jadwal_kuliah WHERE `KODE_PRODI`='$kode_prodi' AND `SEMESTER`='$kode_semester' AND `TAHUN`='$kode_tahun' AND `ID_HARI`='$id_hari' ORDER BY `ID_WAKTU_KULIAH` ASC";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	}

    function getJadwalUjian($kode_prodi,$kode_semester,$kode_tahun)
	{

	    $query = "SELECT * FROM view_jadwal_ujian WHERE `KODE_PRODI`='$kode_prodi' AND `SEMESTER`='$kode_semester' AND `TAHUN`='$kode_tahun' ORDER BY `TANGGAL_UJIAN`,`MULAI` ASC";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	 }

     function getJadwalUjianByTgl($kode_prodi,$kode_semester,$kode_tahun,$index_tgl)
	 {
	    $query = "SELECT * FROM view_jadwal_ujian WHERE TAHUN = '$kode_tahun' AND SEMESTER = '$kode_semester' AND KODE_PRODI='$kode_prodi' AND TANGGAL_UJIAN='$index_tgl' ORDER BY `KODE_MK`,`MULAI` ASC";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	 }

     function getDistinctJadwalUjian($kode_semester,$kode_tahun,$kode_prodi)
     {
        $sql = "SELECT DISTINCT TANGGAL_UJIAN FROM view_jadwal_ujian WHERE SEMESTER={$kode_semester} AND TAHUN ={$kode_tahun} AND KODE_PRODI ={$kode_prodi} ORDER BY TANGGAL_UJIAN ASC";
        $query = $this->db->query($sql);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;

     }

     function getDaftarMkPresensi($kode_semester,$kode_tahun,$nim)
	 {
	    $query = "SELECT * FROM view_presensi WHERE `KODE_SEMESTER`='$kode_semester' AND `TAHUN_AJARAN`='$kode_tahun' AND `NIM`='$nim'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }

      function getPresensiData($tahun,$kode_semester,$kode_mk)
	  {
	    $query = "SELECT * FROM view_presensi where `TAHUN_AJARAN`='$tahun' and `KODE_SEMESTER`='$kode_semester' and `KODE_MK`='$kode_mk'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }

      function getPresensiDataNim($nim,$tahun,$kode_semester,$kode_mk)
	  {
	    $query = "SELECT * FROM view_presensi where `NIM`='$nim' and `TAHUN_AJARAN`='$tahun' and `KODE_SEMESTER`='$kode_semester' and `KODE_MK`='$kode_mk'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }
        else $data = NULL;

        return $data;
	  }

      function getViewKhsAll($nim,$tahun,$kode_semester)
	  {
	    $query = "SELECT * FROM view_khs_all WHERE `NIM`='$nim' and `TAHUN_AJARAN`='$tahun' and `ID_SEMESTER`='$kode_semester' ORDER BY NAMA_MK ASC";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }

      function getViewKhsAllByNim($nim)
	  {
	    $query = "SELECT * FROM view_khs_all where `NIM`='$nim' ORDER BY KODE_MK ASC";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }

      function getViewKhsAllDistinct($nim)
	  {
	    $query = "SELECT DISTINCT(KODE_MK) AS KODE_MK, NAMA_MK, SKS FROM view_khs_all where `NIM`='$nim' ORDER BY KODE_MK ASC";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }

      function getViewKhsAllByNim_nilaiTerbaik($nim,$kode_mk)
	  {
	    $query = "SELECT * FROM view_khs_all
                  WHERE
                   `NIM`='$nim' and
                    KODE_MK ='$kode_mk' and
                    ID_NILAI = (SELECT max(ID_NILAI) FROM view_khs_all WHERE KODE_MK = '$kode_mk' and ID_NILAI <> 99)
                  ORDER BY KODE_MK ASC";

        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }
        else $data = NULL;

        return $data;
	  }

      function getViewKhsAllByKodeMk($nim,$kode_mk)
	  {
	    $query = "SELECT * FROM view_khs_all where `NIM`='$nim' and KODE_MK = '$kode_mk' ORDER BY ID ASC";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }

      function countViewKhsAllByKodeMk($nim,$kode_mk)
	  {
	    $query = "SELECT * FROM view_khs_all where `NIM`='$nim' and KODE_MK = '$kode_mk' ORDER BY ID ASC";
        $query = $this->db->query($query);

        return $query->num_rows();
	  }

      function getViewKrsAll($nim,$tahun,$kode_semester)
	  {
	    $query = "SELECT * FROM view_krs_all WHERE `NIM`='$nim' and `TAHUN_AJARAN`='$tahun' and `ID_SEMESTER`='$kode_semester' ORDER BY NAMA_MK ASC";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }

      function getViewKrsTemp($nim,$kode_semester,$tahun)
	  {
	    $query = "SELECT * FROM view_krs_temp where `NIM_MAHASISWA`='$nim' AND SEMESTER='$kode_semester' AND TAHUN_AJARAN='$tahun'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }

      function dataMatakuliahTersediaById($kode_mkb)
	  {
	    $query = "SELECT * FROM view_mk_tersedia where `ID_TERSEDIA`='$kode_mkb'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }
        else $data = NULL;

        return $data;
	  }

      function dataJamKuliah($id_mk_tersedia)
	  {

	    $query = "SELECT ID_HARI, MULAI, SELESAI FROM view_jadwal_kuliah WHERE `ID_MK_TERSEDIA`='$id_mk_tersedia'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->row();
        }

        return $data;
	  }

      function getViewJadwalKuliah($kode_tahun, $kode_semester, $kode_prodi)
	  {
	    $query = "SELECT * FROM view_jadwal_kuliah WHERE TAHUN = '$kode_tahun' AND SEMESTER = '$kode_semester' AND KODE_PRODI='$kode_prodi' AND KELAS<>'P' ORDER BY ID_HARI,MULAI";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
	  }

      function jumlahPeminatMk($id_mk_tersedia)
	  {
	    $query = "SELECT * FROM temporary_enrollments WHERE ID_MK_TERSEDIA = '$id_mk_tersedia'";
        $query = $this->db->query($query);

        return $query->num_rows();
	  }

      function viewMkTersedia_cekPrasyarat($kode_prasyarat1, $kode_prasyarat2)
      {
        $query = "SELECT `ID_TERSEDIA` FROM view_mk_tersedia WHERE NO_MK= '$kode_prasyarat1' OR NO_MK = '$kode_prasyarat2'";
        $query = $this->db->query($query);

        if($query->num_rows()>0){
          $data = $query->result();
        }
        else $data = NULL;

        return $data;
      }

      function viewDataEnrollment($id_tersedia,$nim)
      {
        $query = "SELECT * FROM view_data_enrollment WHERE ID_MK_TERSEDIA = '$id_tersedia' AND NIM = '$nim'";
        $query = $this->db->query($query);

        return $query->num_rows();
      }

      function getKodeMkByNoMk($no_mk=NULL)
      {
        $query = "SELECT * FROM matakuliah WHERE NO = '$no_mk'";
        $query = $this->db->query($query);

        return $query->row();
      }

      function insertDataKrs($tersedia, $nim)
      {
        $query = "INSERT INTO temporary_enrollments VALUES ('{$tersedia}','{$nim}','')";
        $query = $this->db->query($query);
      }

      function dataDeleteKrs($nim, $id_mk_tersedia)
      {
        $query = "DELETE FROM temporary_enrollments WHERE `NIM_MAHASISWA`='$nim' AND `ID_MK_TERSEDIA`='$id_mk_tersedia' AND `APPROVED` <> 1";
        $query = $this->db->query($query);
      }

      function cekValidasi($nim,$kode_semester,$kode_tahun)
      {
        $query = "SELECT * FROM view_krs_temp where `NIM_MAHASISWA`='$nim' AND SEMESTER='$kode_semester' AND TAHUN_AJARAN='$kode_tahun' AND APPROVED = 1";
        $query = $this->db->query($query);

        if($query->num_rows())
          $data = $query->row();
        else
          $data = NULL;

        return $data;
      }

      function getNeraca($nim)
      {
        $query = "SELECT * FROM view_data_neraca WHERE `NIM`='$nim' ORDER BY `NAMA_TAHUN`";
        $query = $this->db->query($query);

        if($query->num_rows())
          $data = $query->row();
        else
          $data = NULL;

        return $data;
      }
}

